package com.example.mannortycoon.controller;

import com.example.mannortycoon.entity.SysRoleEntity;
import com.example.mannortycoon.service.SysRoleMenuService;
import com.example.mannortycoon.service.SysRoleService;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiIgnore
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    private final SysRoleMenuService sysRoleMenuService;

    @Autowired
    public SysRoleController(SysRoleService sysRoleService,SysRoleMenuService sysRoleMenuService){
        this.sysRoleService = sysRoleService;
        this.sysRoleMenuService = sysRoleMenuService;
    }



    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysRoleService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 角色列表
     */
    @GetMapping("/select")
    public R select(){
        Map<String, Object> map = new HashMap<>();
        //如果不是超级管理员，则只查询自己所拥有的角色列表
//        if(getUserId() != Constant.SUPER_ADMIN){
//            map.put("create_user_id", getUserId());
//        }
        List<SysRoleEntity> list = (List<SysRoleEntity>) sysRoleService.listByMap(map);
        return R.ok().put("list", list);
    }

    /**
     * 角色信息
     */
    @GetMapping("/info/{roleId}")
    public R info(@PathVariable("roleId") Long roleId){
        SysRoleEntity role = sysRoleService.getById(roleId);
        //查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        return R.ok().put("role", role);
    }

    /**
     * 保存角色
     */
    @PostMapping("/save")
    public R save(@RequestBody SysRoleEntity role){
//        ValidatorUtils.validateEntity(role);
        role.setCreateUserId(1L);
//        role.setCreateUserId(getUserId());
        sysRoleService.saveRole(role);

        return R.ok();
    }

    /**
     * 修改角色
     */
    @PostMapping("/update")
    public R update(@RequestBody SysRoleEntity role){
//        ValidatorUtils.validateEntity(role);
        role.setCreateUserId(1L);
//        role.setCreateUserId(getUserId());
        sysRoleService.update(role);
        return R.ok();
    }

    /**
     * 删除角色
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] roleIds){
        sysRoleService.deleteBatch(roleIds);
        return R.ok();
    }

}
